package controllers

import (
	"encoding/json"
	"golanger.com/utils"
	. "helper"
	"jk/jklog"
	. "models"
	"net/http"
	"time"
)

type PageShiCi struct {
	Application
}

func init() {
	App.RegisterController("shici/", PageShiCi{})
}

func (p *PageShiCi) Index(w http.ResponseWriter, r *http.Request) {

	shici := NewJKShiCi()
	var items []JKShiCiContentItem
	err := shici.JKGetshicis(nil, &items)
	if err != nil {
		jklog.L().Errorln(err)
	}

	body := utils.M{}
	body["shici"] = items
	p.Body = body
}

func (p *PageShiCi) Add(w http.ResponseWriter, r *http.Request) {
	// Use for page display
}

func (p *PageShiCi) AddNew(w http.ResponseWriter, r *http.Request) {

	status := utils.M{
		"status": 0,
	}

	if r.Method == "POST" {
		if _, ok := p.POST["ajax"]; ok {

			if p.POST["jkcode"] != "jkadd0702" {
				status["status"] = JK_RESULT_E_CODE_ERROR
				goto out
			}

			query := utils.M{
				"name":   p.POST["name"],
				"title":  p.POST["title"],
				"author": p.POST["author"],
			}

			shici := NewJKShiCi()
			e := shici.JKshiciExist(query)
			if e == true {
				status["status"] = JK_RESULT_E_HAS_EXIST
			} else {

				item := JKShiCiContentItem{
					Name:   p.POST["name"],
					Title:  p.POST["title"],
					Author: p.POST["author"],
				}

				item.RecordTime = time.Now().Unix()
				// create time need convert
				t, err := ConvertStringToUnixTime(p.POST["createtime"])
				if err != nil {
					jklog.L().Errorln("Convert err: ", err)
					item.CreateTime = time.Now().Unix()
				} else {
					if t >= time.Now().Unix() {
						status["status"] = JK_RESULT_E_TIME_FAST
						goto out
					}
					item.CreateTime = t
				}
				item.Content = p.POST["content"]

				err = shici.JKshiciInsert(item)
				if err != nil {
					jklog.L().Errorln("insert err ", err)
					status["status"] = JK_RESULT_E_DATABASE_INSERT_ERROR
				}
			}
		}
	}

out:
	jklog.L().Debug("result: ", status["status"])
	result, _ := json.Marshal(status)
	w.Write(result)
}

func (p *PageShiCi) ListOne(w http.ResponseWriter, r *http.Request) {

	status := utils.M{
		"status": 0,
		"data":   "",
	}

	var qResult JKShiCiContentItem
	if r.Method == "POST" {
		if _, ok := p.POST["ajax"]; ok {
			item := utils.M{
				"name":  p.POST["name"],
				"title": p.POST["title"],
			}

			shici := NewJKShiCi()
			e := shici.JKshiciExist(item)
			if e == false {
				status["status"] = JK_RESULT_E_NOT_EXIST
			} else {
				err := shici.JKshiciQuery(item, &qResult)
				if err != nil {
					jklog.L().Errorln("Query shici err: ", err)
					status["status"] = JK_RESULT_E_DATABASE_QUERY_ERROR
				} else {
					status["data"] = qResult
					status["status"] = 1 // only let ui page don't known what it is and don't deal it
				}
			}
		}
	}

	jklog.L().Debug("result: ", status["status"])
	result, _ := json.Marshal(status)
	w.Write(result)
}

func (p *PageShiCi) RemoveOne(w http.ResponseWriter, r *http.Request) {
	status := utils.M{
		"status": 0,
	}

	if r.Method == "POST" {
		if _, ok := p.POST["ajax"]; ok {

			item := utils.M{
				"name":  p.POST["name"],
				"title": p.POST["title"],
			}

			shici := NewJKShiCi()
			e := shici.JKshiciExist(item)
			if e == false {
				status["status"] = JK_RESULT_E_DATA_NOT_EXIST
			} else {
				err := shici.JKshiciRemove(item)
				if err != nil {
					jklog.L().Errorln("Remove shi ci err:", err)
					status["status"] = JK_RESULT_E_DATABASE_REMOVE_ERROR
				}
			}
		}
	}

	jklog.L().Debug("result: ", status["status"])
	result, _ := json.Marshal(status)
	w.Write(result)
}
